// var originalOnGetNode = frappe.treeview_settings["Account"].on_get_node
// console.log(`originalOnGetNode 👉`, originalOnGetNode)
frappe.treeview_settings["Account"].on_get_node = async function (nodes, deep = false) {
    if (frappe.boot.user.can_read.indexOf("GL Entry") == -1) return
    const 改色否 = await frappe.db.get_single_value("shezhi", "kemubiaoshujiegou_gaise")
    let accounts = []
    if (deep) {
        accounts = nodes.reduce((acc, node) => [...acc, ...node.data], [])
    } else {
        accounts = nodes
    }

    frappe.db.get_single_value("Accounts Settings", "show_balance_in_coa").then(value => {
        if (value) {
            const get_balances = frappe.call({
                method: "erpnext.accounts.utils.get_account_balances",
                args: {
                    accounts: accounts,
                    company: cur_tree.args.company,
                },
            })

            get_balances.then(async r => {
                if (!r.message || r.message.length == 0) return

                for (let account of r.message) {
                    const node = cur_tree.nodes && cur_tree.nodes[account.value]
                    if (!node || node.is_root) continue

                    // show Dr if positive since balance is calculated as debit - credit else show Cr
                    const balance = account.balance_in_account_currency || account.balance

                    if (改色否) {
                        // const dr_or_cr = balance > 0 ? "借" : "贷"
                        const { 借贷, color } = amtf.util.成借贷(balance)

                        const format = (value, currency) => format_currency(Math.abs(value), currency)

                        if (account.balance !== undefined) {
                            node.parent && node.parent.find(".balance-area").remove()
                            $(
                                `<span class="balance-area pull-right" style="color: ${color} !important;">` +
                                    (account.balance_in_account_currency
                                        ? format(account.balance_in_account_currency, account.account_currency) +
                                          " / "
                                        : "") +
                                    format(account.balance, account.company_currency) +
                                    " " +
                                    借贷 +
                                    "</span>"
                            ).insertBefore(node.$ul)
                        }
                    } else {
                        const dr_or_cr = balance > 0 ? "Dr" : "Cr"
                        const format = (value, currency) => format_currency(Math.abs(value), currency)

                        if (account.balance !== undefined) {
                            node.parent && node.parent.find(".balance-area").remove()
                            $(
                                '<span class="balance-area pull-right">' +
                                    (account.balance_in_account_currency
                                        ? format(account.balance_in_account_currency, account.account_currency) +
                                          " / "
                                        : "") +
                                    format(account.balance, account.company_currency) +
                                    " " +
                                    dr_or_cr +
                                    "</span>"
                            ).insertBefore(node.$ul)
                        }
                    }
                    // const dr_or_cr = balance > 0 ? "Dr" : "Cr"
                }
            })
        }
    })
}
